#!/bin/bash
set -e
#获取后缀
paramester="$1"

#menu
function menu()
{
        echo -e "*******************************************"
        echo -e "* Welcome to download OpenHarmony Code    *"
        echo -e "* Please Choice OpenHarmony SDK:          *"
        echo -e "* OpenHarmony Version                     *"
        echo -e "*   OpenHarmony master          prese 1.1 *"
        echo -e "*   OpenHarmony-3.2-beta2       press 1.2 *"
        echo -e "*   OpenHarmony-3.2-beta2-merge press 1.3 *"
        echo -e "*   OpenHarmony-3.2-release     press 1.4 *"
        echo -e "*******************************************"
}

#打印版本信息
function information()
{
        echo "======================版本信息==========================="
        if [ "$paramester" = '1.1' ]
        then
                echo "url       : https://gitee.com/openharmony/manifest.git"
                var_1="https://gitee.com/openharmony/manifest.git"
        else
                var_1="https://gitee.com/openharmony/manifest"
                echo "url       : https://gitee.com/openharmony/manifest"
        fi
        echo "branch    : $name"
        if [ "$paramester" = '1.1' -o "$paramester" = '1.2' ]
        then
                echo "xml_name  :"
                var_2=""
        elif [ "$paramester" = '1.3' ]
        then
                var_2="thead_develop.xml"
                echo "xml_name  : thead_develop.xml"
        else
                var_2=
                echo "xml_name  : default.xml"
        fi
        echo "code_path : $pwd"
        echo "init   cmd: repo init -u $var_1 -b $name -m $var_2 --no-repo-verify"
        echo "sync      cmd: repo sync -c"
        echo "lfs       cmd: repo forall -c 'git lfs pull'"
        echo "set_br    cmd: repo start $name --all"
        exit 0
}

#创建文件夹
function create()
{
        if [ ! -e "$name" ]
        then
                mkdir $name
		cd $name
                pwd=$(pwd)
        elif [ -e ${name}_tmp ]
        then
                rmdir ${name}_tmp
                mkdir ${name}_tmp
                cd ${name}_tmp
                pwd=`pwd`
        else
                mkdir ${name}_tmp
                cd ${name}_tmp
                pwd=`pwd`
        fi
}

#检测输入是否为空
function check()
{
        if [ ! "$paramester" ]
        then
                echo "================请输入参数!================"
                menu
                exit 0
        fi
}

#打印下载命令
function order()
{
        if [ "$paramester" = '1.1' -o "$paramester" == 'master' ]
        then
                        paramester='1.1'
                        echo "=====================下载命令============================ "
                        name="master"
                        echo "repo init -u https://gitee.com/openharmony/manifest.git -b master --no-repo-verify
repo sync -c
repo forall -c 'git lfs pull'
repo start master --all"
                        create
        elif [ "$paramester" = '1.2' -o "$paramester" == 'OpenHarmony-3.2-beta2' ]
        then
                        paramester='1.2'
                        echo "=====================下载命令============================ "
                        name="OpenHarmony-3.2-beta2"
                        echo "repo init -u https://gitee.com/openharmony/manifest -b OpenHarmony-3.2-Beta2 --no-repo-verify
repo sync -c
repo forall -c 'git lfs pull'
repo start OpenHarmony-3.2-Beta2 --all"
                        create
        elif [ "$paramester" = '1.3' -o "$paramester" == 'OpenHarmony-3.2-beta2-merge' ]
        then
                        paramester='1.3'
                        echo "=====================下载命令============================ "
                        name="OpenHarmony-3.2-beta2-Merge"
                        echo "repo init -u https://gitee.com/openharmony/manifest -b OpenHarmony-3.2-beta2-Merge -m thead_develop.xml --no-repo-verify
repo sync -c
repo forall -c 'git lfs pull'
repo start OpenHarmony-3.2-beta2 --all"
                        create
        elif [ "$paramester" = '1.4' -o "$paramester" == 'OpenHarmony-3.2-release' ]
        then
                        paramester='1.4'
                        echo "=====================下载命令============================ "
                        name="OpenHarmony-3.2-Release"
                        echo "repo init -u https://gitee.com/openharmony/manifest -b OpenHarmony-3.2-Release -m default.xml --no-repo-verify
repo sync -c
repo forall -c 'git lfs pull'
repo start  OpenHarmony-3.2-Release --all"
                        create
        else

                        echo "product is not in product list, please recheck!!!"
                        menu
                        exit 0
        fi
}
#主函数
function main()
{
        #开始检测
        check
        #打印命令
        order
        #打印版本信息
        information
}
main

